const express = require("express");
const db = require("../db/index")
const router = express.Router();

const multer = require('multer')
const path = require("path")


// 配置mutler的池
let storage = multer.diskStorage({
    // 把图片放在那里
    destination: function (req, file, cb) {
        // NodeJS的两个全局变量
        // console.log(__dirname);  //获取当前文件在服务器上的完整目录 
        // console.log(__filename); //获取当前文件在服务器上的完整路径 
        cb(null, path.join(__dirname, "../static/image"))
    },
    // 改变图片的名字
    filename: function (req, file, cb) {
        cb(null, Date.now() + path.extname(file.originalname))
    }

})

const image = multer({ storage: storage })

router.post("/alongUpload", image.single("avator"), (req, res) => {
    
    if (req.file) {
       res.send({
        status:200,
        mess:"上传成功",
        imgName:req.file.filename
       })
    } else {
        res.send({
            status: 401,
            message: '上传失败',
        })
    }
})

module.exports = router


